/**
 * Created by WYH on 17/4/14.
 */
angular.module('toolBar', []).component(
    'toolBar',
    {
        templateUrl: 'component/base/toolBar/tool_bar.template.html',
        controller: ['$scope', 'ElePosition', 'EleStyle', 'Event', function ($scope, ElePosition, EleStyle, Event) {

            $scope.showOptions = false;

            $scope.filterValue = null;
            $scope.mouseClick=false;

            $scope.optionItems = [{key: 'all', name: '全部'}].concat($scope.$parent.optionItems);


            $scope.selectOption = $scope.optionItems[0];

            $scope.submitFilter = submitFilter;

            $scope.clickAdd = clickAdd;

            $scope.changeMouseClick=changeMouseClick;
            function changeMouseClick() {
                document.getElementById('searchInputCount').removeAttribute('placeholder')
                $scope.mouseClick=true;
            }
            //watchKey();

            $scope.changeSelectOption = changeSelectOption;
            if (!$scope.$parent.hideSearch) {
                setupOptions();
                changeOptionsWhenScreenResize();
            }


            //function watchKey() {
            //    $scope.$watch('filterValue', function () {
            //        window.document.onkeydown = disableRefresh;
            //        function disableRefresh(evt) {
            //            evt = (evt) ? evt : window.event
            //            if (evt.keyCode) {
            //                if (evt.keyCode == 13) {
            //                    submitFilter();
            //                }
            //            }
            //        }
            //    })
            //}

            function changeSelectOption(option) {
                $scope.selectOption = option;
                $scope.showOptions = false;
                $scope.shouldBeOpen = !$scope.shouldBeOpen;

            }

            function changeOptionsWhenScreenResize() {
                Event.addEvent(window, 'resize', function () {

                          setupOptions();


                });
            }

            function clickAdd() {
                $scope.$parent.clickAdd();
            }


            function hideOptionsWhenSelectOther() {
                $scope.$apply(function () {
                    $scope.showOptions = false;
                });

            }


            function submitFilter() {
                var filterKey = $scope.selectOption.key;
                var filterValue = $scope.filterValue;
                $scope.$parent.submitFilter(filterKey, filterValue);
            }


            function setupOptions() {
                var $filterSelect = $("#toolFilterSelect");
                var $toolOptionsBox = $("#toolOptionsBox");
                if($filterSelect.length !==0 && $toolOptionsBox.length!==0){
                    $toolOptionsBox.css('top', '123px');
                    $toolOptionsBox.css('width', '130px');
                    $toolOptionsBox.css('left', $filterSelect.offset().left + 'px');
                }


                // var eleSelect = document.getElementById('toolFilterSelect');
                // var eleOptionBox = document.getElementById('toolOptionsBox');
                // if (eleSelect) {
                //     if (eleSelect.clientWidth == 0) hideOptionsWhenSelectOther();
                //     eleOptionBox.style.top = ElePosition.getElementViewTop(eleSelect) + eleSelect.clientHeight + 'px';
                //
                //     eleOptionBox.style.left = ElePosition.getElementViewLeft(eleSelect) + 'px';
                //     eleOptionBox.style.width = '130px';
                // }
            }


        }]
    }
);